﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Fast
{
    [SkipScan]
    public static class JwtBearerFastApplicationExtensions
    {
        /// <summary>
        /// 添加Bearer认证
        /// </summary>
        /// <param name="application"></param>
        /// <param name="authenticationConfigure"></param>
        /// <param name="jwtBearerConfigure"></param>
        /// <returns></returns>
        public static IFastApplication AddJwtWithBearerAuthentication(
            this IFastApplication application,
            Action<AuthenticationOptions> authenticationConfigure = null,
            Action<JwtBearerOptions> jwtBearerConfigure = null
        )
        {
            application.Services.AddJwtWithBearerAuthentication(authenticationConfigure, jwtBearerConfigure);

            return application;
        }

        /// <summary>
        /// 添加Bearer认证并携带策略授权
        /// </summary>
        /// <typeparam name="TAuthorizationHandler">策略授权处理器</typeparam>
        /// <param name="application"></param>
        /// <param name="authenticationConfigure"></param>
        /// <param name="jwtBearerConfigure"></param>
        /// <param name="enableGlobalAuthorize"></param>
        /// <returns></returns>
        public static IFastApplication AddJwtWithBearerAuthentication<TAuthorizationHandler>(
            this IFastApplication application,
            Action<AuthenticationOptions> authenticationConfigure = null,
            Action<JwtBearerOptions> jwtBearerConfigure = null,
            bool enableGlobalAuthorize = false
        ) where TAuthorizationHandler : class, IAuthorizationHandler
        {
            application.Services.AddJwtWithBearerAuthentication<TAuthorizationHandler>(authenticationConfigure, jwtBearerConfigure, enableGlobalAuthorize);

            return application;
        }
    }
}
